#!/bin/bash
# 安装PHP

# 引入公共配置
source ./common.sh

echo "开始安装PHP..."

read -p "输入需要的php版本(7.2或者8.0 默认是8.0版本): " Version
if [ -z "${Version}" ];then
    Version="8.0"
    echo "使用默认版本: $Version"
fi

# 停止正在运行的php-fpm
systemctl stop php-fpm 2>/dev/null

if [ "${Version}" == "7.2" ];then
    echo "安装7.2版本的php"
    # 卸载可能存在的8.0版本
    yum -y remove php php-{cli,fpm,mysqlnd,zip,devel,gd,mbstring,curl,xml,pear,bcmath,json} 2>/dev/null
    
    # 安装源
    yum -y install http://repo.webtatic.com/yum/el7/x86_64/RPMS/webtatic-release-7-3.noarch.rpm
    rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm 2>/dev/null
    
    # 安装PHP 7.2
    yum-config-manager --disable 'remi-php*'
    yum-config-manager --enable remi-php72
    yum -y install php72w-cli php72w-fpm
    
else
    echo "安装8.0版本的php"
    # 卸载可能存在的7.2版本
    yum -y remove php72w-cli php72w-fpm 2>/dev/null
    
    # 安装源
    yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
    yum -y install https://rpms.remirepo.net/enterprise/remi-release-7.rpm
    yum -y install yum-utils
    
    # 安装PHP 8.0
    yum-config-manager --disable 'remi-php*'
    yum-config-manager --enable remi-php80
    yum -y install php php-{cli,fpm,mysqlnd,zip,devel,gd,mbstring,curl,xml,pear,bcmath,json}
fi

# 启动并设置开机自启
systemctl start php-fpm
systemctl enable php-fpm

# 检查安装结果
php -v
if [ $? -eq 0 ];then
    success_msg "PHP ${Version} 安装成功"
else
    error_exit "PHP ${Version} 安装失败"
fi
